﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace HospitalSystem
{
    public partial class cal : System.Web.UI.Page
    {
        DataClasses1DataContext db = new DataClasses1DataContext();
        int CaseNum = 0;
        int CasenumInInt = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack){
            Panel1.Visible = false;
            ResetValue();
            string CID = (String)Session["CID"];
            var query = from a in db.Persons
                        where CID.Equals(a.CID)
                        select a;
            //Label10.ForeColor = Color.Green;
            //Label10.Text = "OK";
            Panel1.Visible = true;
            foreach (var q in query)
            {
                Label1.Text = q.FirstName;
                Label2.Text = q.LastName;
                //int age = DateTime.Now.Year - q.DateofBirth.Year;
                //if (DateTime.Now < q.DateofBirth.AddYears(age))
                //{
                // age--;
                //}
                //Label3.Text = age.ToString();
            }
            Label9.ForeColor = Color.Green;
            Label9.Text = "OK";
            showStaffName();
            showHostname();
            }
            if (Session["CID"] == null)
            {

                MessageBox2("กรุณากดค้นหา หรือลงทะเบียนการคัดกรองเด็กก่อน", "checkcid.aspx");

            }


        }

        private void MessageBox2(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }

        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }


        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            string selectedValue = DropDownList1.SelectedValue;
            if (selectedValue == "0")
            {
                Label4.BackColor = Color.Red;
                Label4.Text = "No";
                //Literal1.Text = "<p style='color:Red;'>No</p>";
            }
            else
            {
                if (selectedValue.Equals(DropDownList2.SelectedValue) ||
                    selectedValue.Equals(DropDownList3.SelectedValue) ||
                    selectedValue.Equals(DropDownList4.SelectedValue) ||
                    selectedValue.Equals(DropDownList7.SelectedValue))
                {
                    Label4.ForeColor = Color.Red;
                    Label4.Text = "No";
                }
                else
                {
                    Label4.ForeColor = Color.Green;
                    Label4.Text = "OK";
                    //Literal4.Text = "<p style='color:Green;'>OK</p>"; ;
                }
            }
        }

        protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            string selectedValue = DropDownList2.SelectedValue;
            if (selectedValue == "0")
            {
                Label5.BackColor = Color.Red;
                Label5.Text = "No";
                //Literal1.Text = "<p style='color:Red;'>No</p>";
            }
            else
            {
                if (selectedValue.Equals(DropDownList1.SelectedValue) ||
                    selectedValue.Equals(DropDownList3.SelectedValue) ||
                    selectedValue.Equals(DropDownList4.SelectedValue) ||
                    selectedValue.Equals(DropDownList7.SelectedValue))
                {
                    Label5.ForeColor = Color.Red;
                    Label5.Text = "No";
                }
                else
                {
                    Label5.ForeColor = Color.Green;
                    Label5.Text = "OK";
                    //Literal4.Text = "<p style='color:Green;'>OK</p>"; ;
                }
            }
        }

        protected void DropDownList3_SelectedIndexChanged1(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            string selectedValue = DropDownList3.SelectedValue;
            if (selectedValue == "0")
            {
                Label6.BackColor = Color.Red;
                Label6.Text = "No";
                //Literal1.Text = "<p style='color:Red;'>No</p>";
            }
            else
            {
                if (selectedValue.Equals(DropDownList2.SelectedValue) ||
                    selectedValue.Equals(DropDownList1.SelectedValue) ||
                    selectedValue.Equals(DropDownList4.SelectedValue) ||
                    selectedValue.Equals(DropDownList7.SelectedValue))
                {
                    Label6.ForeColor = Color.Red;
                    Label6.Text = "No";
                }
                else
                {
                    Label6.ForeColor = Color.Green;
                    Label6.Text = "OK";
                    //Literal4.Text = "<p style='color:Green;'>OK</p>"; ;
                }
            }
        }

        protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            string selectedValue = DropDownList4.SelectedValue;
            if (selectedValue == "0")
            {
                Label7.BackColor = Color.Red;
                Label7.Text = "No";
                //Literal1.Text = "<p style='color:Red;'>No</p>";
            }
            else
            {
                if (selectedValue.Equals(DropDownList2.SelectedValue) ||
                    selectedValue.Equals(DropDownList3.SelectedValue) ||
                    selectedValue.Equals(DropDownList1.SelectedValue) ||
                    selectedValue.Equals(DropDownList7.SelectedValue))
                {
                    Label7.ForeColor = Color.Red;
                    Label7.Text = "No";
                }
                else
                {
                    Label7.ForeColor = Color.Green;
                    Label7.Text = "OK";
                    //Literal4.Text = "<p style='color:Green;'>OK</p>"; ;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Evaluation CN = new Evaluation();
            string CID = (String)Session["CID"];
            if (Label4.Text == "OK" &&
                Label5.Text == "OK" &&
                Label6.Text == "OK" &&
                Label7.Text == "OK" &&
                Label8.Text == "OK" &&
                Label9.Text == "OK")
            {
                var query = from a in db.Evaluations
                            orderby a.CaseNo
                            where a.CID.Equals(CID)
                            select a.CaseNo;
                if (query.FirstOrDefault() == null)
                {
                    CaseNum = 1;
                }
                else
                {
                    foreach (var q in query)
                    {
                        CaseNum = q;
                    }
                    CaseNum++;
                }
               
                CN.CID = CID;
                CN.CaseNo = CaseNum;
                CN.StaffCode = DropDownList1.SelectedValue;
                CN.Co1Code = DropDownList2.SelectedValue;
                CN.Co2Code = DropDownList3.SelectedValue;
                CN.Co3Code = DropDownList4.SelectedValue;
                CN.Co4Code = DropDownList7.SelectedValue;
                CN.HostID = DropDownList8.SelectedValue;
                CN.EvalDateTime = DateTime.Now;
                db.Evaluations.InsertOnSubmit(CN);
                try
                {
                    db.SubmitChanges();
                    Session["CaseNo"] = CaseNum.ToString();
                    MessageBox2("กำลังไปขั้นตอนถัดไป OK CaseNumber :" + CaseNum , "diagnosemedical.aspx");
                   
               
                }
                catch
                {
                    MessageBox("Error");
                }

            }
            else
            {
                MessageBox("ไม่สามารุถบันทึกข้อมูลได้ เนื่องจากกรอกข้อมูลผิดพลาด");
            }
        }


        private void showStaffName()
        {
            var query = from a in db.Staffs
                        select new
                        {
                            a.StaffID,
                            a.StaffName
                        };
            foreach (var q in query)
            {
                DropDownList1.Items.Add(new ListItem(q.StaffName, q.StaffID));
                DropDownList2.Items.Add(new ListItem(q.StaffName, q.StaffID));
                DropDownList3.Items.Add(new ListItem(q.StaffName, q.StaffID));
                DropDownList4.Items.Add(new ListItem(q.StaffName, q.StaffID));
                DropDownList7.Items.Add(new ListItem(q.StaffName, q.StaffID));
            }
        }

        //For Show Hostname @DropdownList10
        //Text is Description (Hostinfo DB)
        //Value is HostID (Hostinfo DB)
        private void showHostname()
        {
            //Panel1.Visible = true;
            DropDownList8.Items.Add(new ListItem("==เลือกสถานที่ฝึกเด็ก==", "NoValue"));
            var query = from a in db.Hosts
                        select a;
            foreach (var q in query)
            {
                DropDownList8.Items.Add(new ListItem(q.HostName, q.HostID));
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            
        }

        private void ResetValue()
        {
            DropDownList1.Items.Clear();
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            DropDownList4.Items.Clear();
            DropDownList7.Items.Clear();
            DropDownList8.Items.Clear();
        }

        protected void DropDownList7_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            string selectedValue = DropDownList7.SelectedValue;
            if (selectedValue.Equals(DropDownList1.SelectedValue) ||
                    selectedValue.Equals(DropDownList2.SelectedValue) ||
                    selectedValue.Equals(DropDownList3.SelectedValue) ||
                    selectedValue.Equals(DropDownList4.SelectedValue))
            {
                Label8.ForeColor = Color.Red;
                Label8.Text = "No";
            }
            else
            {
                Label8.ForeColor = Color.Green;
                Label8.Text = "OK";
            }
        }

        protected void DropDownList8_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            string selectedValue = DropDownList8.SelectedValue;
            if (selectedValue == "0")
            {
                Label9.ForeColor = Color.Red;
                Label9.Text = "NO";
            }
            else
            {
                Label9.ForeColor = Color.Green;
                Label9.Text = "OK";
            }
        }

        


    }
}
